import { LocalStorageService } from 'src/app/services/local-storage.service';
import { Component, OnInit, ViewChild} from '@angular/core';
import { IonSlides, ToastController } from '@ionic/angular';
import { AuthenticationCodeService } from '../services/authentication-code.service';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Router } from '@angular/router';
@Component({
  selector: 'app-register',
  templateUrl: './register.page.html',
  styleUrls: ['./register.page.scss'],
})
export class RegisterPage implements OnInit {

  

  private userNumber = '';
  private userName = '';
  private userPhone = '';
  private userMail = '';
  private userDate = '';
  private response : any;
  private httpOptions = {
    headers: new HttpHeaders({ 'Content-Type': 'application/json' })
  };

  constructor(public localStorage: LocalStorageService,
              public authenticationCodeService: AuthenticationCodeService,
              public http: HttpClient,
              public router: Router,
              private toastCtrl: ToastController) { }
  ngOnInit() {
    // this.userDate = new Date();
    this.formatDate();

  }

  formatDate(){
    const Dates = new Date();
    const Year : number = Dates.getFullYear(); 
    const Months : any = ( Dates.getMonth() + 1 ) < 10  ?  '0' + (Dates.getMonth() + 1) : ( Dates.getMonth() + 1); 
    const Day : any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
    this.userDate = Year + '-' + Months + '-' + Day;
  }


  saveUser() {
    this.http.post('http://127.0.0.1:8080/pcs/user/register.do',
    {uNumber: this.userNumber, uName: this.userName, phone: this.userPhone, emaile: this.userMail, createDate: this.userDate, status: 1}, this.httpOptions)
      .subscribe(async response => {
        this.response = response;
        console.log(this.response);
        if (this.response.data.user.uId != null) {
          const toast = await this.toastCtrl.create({
                message: '注册成功',
                position: 'top',
                duration: 2000
              });
          await toast.present();
          this.router.navigateByUrl('/login');
        }
        else {
          const toast = await this.toastCtrl.create({
            message: '注册失败',
            position: 'top',
            duration: 2000
          });
          await toast.present();
        }
      }, function(error){console.log(error);});
  
  }

}
